package com.code2roc.jarmanage.servlet.project;

import com.code2roc.jarmanage.bizloigc.ProjectBizloigc;
import com.code2roc.jarmanage.db.GroupDB;
import com.code2roc.jarmanage.db.ProjectDB;
import com.code2roc.jarmanage.depoly.DepolyServiceFactory;
import com.code2roc.jarmanage.depoly.DepolyStatus;
import com.code2roc.jarmanage.model.*;
import com.code2roc.jarmanage.util.CommonUtil;
import com.code2roc.jarmanage.util.ConvertUtil;
import com.code2roc.jarmanage.util.FileUtil;
import com.code2roc.jarmanage.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.ConvolveOp;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

public class ListServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Result result = Result.okResult();
        HashMap paramMap = CommonUtil.getParamMap(req);
        String sql = "1=1 and (DepolyType is null or DepolyType='')";
        String projectName = ConvertUtil.convert2String(paramMap.get("projectName"));
        if (!StringUtil.isEmpty(projectName)) {
            sql += " and projectName like '%" + projectName + "%'";
        }
        String groupGuid = ConvertUtil.convert2String(paramMap.get("groupGuid"));
        if (!StringUtil.isEmpty(groupGuid)) {
            String groupsql = "and ( ";
            String[] groupList = groupGuid.split("\\|");
            for (int i = 0; i < groupList.length; i++) {
                groupsql += " groupGuid like '%" + groupList[i] + "%'  or";
            }
            groupsql = groupsql.substring(0, groupsql.length() - 2);
            groupsql += " ) ";
            sql += groupsql;
        }
        List<ProjectDO> entityList = ProjectDB.selectList(sql);
        List<ProjectModel> projectModelList = new ArrayList<>();
        for (ProjectDO entity : entityList) {
            ProjectModel model = new ProjectModel();
            model = ConvertUtil.convertSameStructObj(entity, model);
            String filePath = entity.getJarPackagePath() + File.separator + entity.getServiceName() + ".jar";
            if (new File(filePath).exists()) {
                model.setProjectStatus(new DepolyServiceFactory().CreateDeployService().getStatus(model.getServiceName()));
            } else {
                model.setProjectStatus(DepolyStatus.UnUpload);
            }
            List<FileInfo> configFileList = ProjectBizloigc.getConfigFileList(entity.getProjectID());
            model.setConfigFileCount(configFileList.size());
            model.setCommandCount(ProjectBizloigc.getCommondList(entity.getProjectID()).size());
            if (!StringUtil.isEmpty(entity.getGroupGuid())) {
                String groupName = "";
                String[] groupGuidList = entity.getGroupGuid().split("\\|");
                for (int i = 0; i < groupGuidList.length; i++) {
                    GroupDO groupDO = GroupDB.selectOne(groupGuidList[i]);
                    if (groupDO != null) {
                        groupName += groupDO.getGroupName() + "|";
                    }
                }
                if (groupName.endsWith("|")) {
                    groupName = groupName.substring(0, groupName.length() - 1);
                }
                model.setGroupName(groupName);
            }
            projectModelList.add(model);
        }

        String projectStatus = ConvertUtil.convert2String(paramMap.get("projectStatus"));
        if (!StringUtil.isEmpty(projectStatus) && projectModelList.size()>0) {
            projectModelList = projectModelList.stream().filter(a->a.getProjectStatus().equals(projectStatus)).collect(Collectors.toList());
        }
        result.add("obj", projectModelList);
        resp.getWriter().println(result.toString());
    }
}
